package main;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

public class TableHeader extends PdfPageEventHelper{
	String header;
    PdfTemplate headertemp;
    PdfTemplate footertemp;
    BaseFont baseFont;
    public void setHeader(String header){
     this.header=header;
    }
    public void setBaseFont(BaseFont baseFont){
        this.baseFont=baseFont;
       }
    public void onOpenDocument(PdfWriter writer,Document document){
    	headertemp = writer.getDirectContent().createTemplate(30,16);
    	footertemp = writer.getDirectContent().createTemplate(30,16);
    }
    
    public void onEndPage (PdfWriter writer, Document document) {
     PdfPTable table = new PdfPTable(1);
     try{
     /*table.setWidths(new int[]{24,24,2});*/
     table.setTotalWidth(527);
     table.setLockedWidth(true);
     table.getDefaultCell().setFixedHeight(20);
     table.getDefaultCell().setBorder(Rectangle.BOTTOM);
     Font keyfont=new Font(baseFont,20,Font.BOLD);
     PdfPCell c1 = new PdfPCell(new Paragraph(header,keyfont));
     table.addCell(c1);
     table.getDefaultCell().setHorizontalAlignment(
     Element.ALIGN_RIGHT);
     
    /* keyfont=new Font(baseFont,8,Font.BOLD);
     c1 = new PdfPCell(new Paragraph(String.format("第 %d 页 共",writer.getPageNumber()),keyfont));
     table.addCell(c1);
     PdfPCell cell = new PdfPCell(Image.getInstance(headertemp));
     cell.setBorder(Rectangle.BOTTOM);
     table.addCell(cell);
     table.writeSelectedRows(0,-1,34,803,writer.getDirectContent());*/

     
     PdfPTable foottable = new PdfPTable(3);
     foottable.setWidths(new int[]{24,24,2});
     foottable.setTotalWidth(527);
     foottable.setLockedWidth(true);
     foottable.getDefaultCell().setFixedHeight(20);
     
     keyfont=new Font(baseFont,8,Font.BOLD);
     c1 = new PdfPCell(new Paragraph(String.format("第 %d 页 共",writer.getPageNumber()),keyfont));
     foottable.addCell(c1);
     PdfPCell cell = new PdfPCell(Image.getInstance(footertemp));
     foottable.addCell(cell);
     foottable.writeSelectedRows(0,-1,34,50,writer.getDirectContent());
     
     }
     catch(DocumentException de){
     throw new ExceptionConverter(de);
     }
    }
    
    public void onCloseDocument(PdfWriter writer,Document document){
       // ColumnText.showTextAligned(headertemp,Element.ALIGN_LEFT,new Phrase(String.valueOf(writer.getPageNumber())+" 页"),2,2,0);
        ColumnText.showTextAligned(footertemp,Element.ALIGN_LEFT,new Phrase(String.valueOf(writer.getPageNumber())+" 页"),2,2,0);
    }
}